<template>
  <el-dialog class="form-dialog" title="版本列表" width="888px" :visible.sync="visible" @cancel="close">
    <el-table
      :data="versionList"
      style="width: 100%; overflow: auto"
    >
      <el-table-column
        prop="versionNum"
        width="120"
        align="center"
        label="版本号"
      />

      <el-table-column
        prop="updateMsg"
        label="更新说明"
      >
        <template slot-scope="scope">
          <div class="template-remark">{{ scope.row.updateMsg }}</div>
        </template>
      </el-table-column>

      <el-table-column
        prop="templateUserDesc"
        label="开发说明"
      >
        <template slot-scope="scope">
          <div>【模板ID】{{ scope.row.templateId }}</div>
          <div>【开发版本号】{{ scope.row.templateUserVersion }}</div>
          <div class="t-template-user-desc">【开发备注】{{ scope.row.templateUserDesc }}</div>
        </template>
      </el-table-column>

      <el-table-column
        prop="updateTime"
        width="120"
        align="center"
        label="创建时间"
      />

      <el-table-column
        prop="option"
        align="center"
        width="100"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleSelect(scope.row)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      :current-page.sync="pageNum"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </el-dialog>
</template>

<script>
/* eslint-disable no-console */
import { getVersionList, getVersionPage } from '@/api/wxapp'

export default {
  name: 'SelectVersionDialog',
  data() {
    return {
      loading: false,
      visible: false,
      reject: null,
      resolve: null,
      classifyCode: null,
      templateCode: null,
      pageNum: 1,
      pageSize: 5,
      total: 0,
      versionList: []
    }
  },

  mounted() {
    this.getVersionList()
  },

  methods: {
    handleCurrentChange(cur) {
      this.pageNum = cur
      this.getVersionList()
    },
    getVersionList() {
      const data = {
        classifyCode: this.classifyCode,
        templateCode: this.templateCode,
        pageSize: this.pageSize,
        pageNum: this.pageNum
      }
      return getVersionPage(data).then((resp) => {
        this.versionList = resp.data.list
        this.total = resp.data.total
        return resp
      })
    },
    handleSelect(template) {
      const resolve = this.resolve
      this.resolve = null
      this.reject = null
      resolve && resolve(template)
      this.visible = false
    },
    show(classifyCode, templateCode) {
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
        this.visible = true
        this.classifyCode = classifyCode
        this.templateCode = templateCode
        this.getVersionList()
      })
    },
    close() {
      this.resolve = null
      this.reject = null
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .form-dialog ::v-deep .el-dialog__body {
    padding: 20px 20px 20px 20px !important;
  }

  .t-template-user-desc {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }
  .template-remark {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  .pagination {
    margin-top: 10px;
  }
</style>
